package com.sims.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.sims.bean.Class;
import com.sims.bean.Teacher;
import com.sims.dao.IClassDAO;
import com.sims.dao.ITeacherDAO;
import com.sims.util.Constant;
import com.sims.util.HttpSessionUtil;

public class TeacherService {
	@Autowired
	private ITeacherDAO teacherDAO;
	@Autowired
	private IClassDAO classDAO;
	
	public Map<String, String> teacherLogin(Teacher teacher) {
		Map<String, String> result = new HashMap<String, String>();
		Teacher teacherInDB = teacherDAO.findByTno(teacher.getTno());

		if (teacherInDB == null) {
			result.put(Constant.ERROR, "Wrong Account Or Password");
			return result;
		} else if (!teacherInDB.getPassword().trim()
				.equals(teacher.getPassword())) {
			result.put(Constant.ERROR, "Wrong Account Or Password");
			return result;
		}
		
		com.sims.bean.Class tClass = (Class) classDAO.findByTid(teacherInDB.getTid()).get(0);
		
		if (tClass != null)
			HttpSessionUtil.login(teacherInDB.getTid(), Constant.TEACHER,
					tClass.getCid());
		else HttpSessionUtil.login(teacherInDB.getTid(), Constant.TEACHER, null);
		
		BeanUtils.copyProperties(teacherInDB, teacher);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> teacherLogoff() {
		Map<String, String> result = new HashMap<String, String>();
		HttpSessionUtil.logout();
		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> modifyTeacherPassword(Teacher teacher) {
		Map<String, String> result = new HashMap<String, String>();
		Teacher t = teacherDAO.findById(HttpSessionUtil.getUserId());
		if (t==null) {
			result.put(Constant.ERROR, "Teacher Not Exists");
			return result;
		}
		if(teacher.getPassword().length() != 0)t.setPassword(teacher.getPassword());
		teacherDAO.attachDirty(t);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
	
	
	
	public Map<String, String> findTeacherBasicInfo(Teacher teacher) {
		
		Map<String, String> result = new HashMap<String, String>();
		
		Teacher t = teacherDAO.findById(teacher.getTid());
		if (t==null) {
			result.put(Constant.ERROR, "Teacher Not Exists");
			return result;
		}
		
		BeanUtils.copyProperties(t, teacher);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}

	public Map<String, String> modifyTeacherBasicInfo(Teacher teacher) {
		Map<String, String> result = new HashMap<String, String>();
		Teacher t = teacherDAO.findById(HttpSessionUtil.getUserId());
		if (t==null) {
			result.put(Constant.ERROR, "Teacher Not Exists");
			return result;
		}
		if(teacher.getBirthday() != null)t.setBirthday(teacher.getBirthday());
		if(teacher.getIdcard().length() != 0)t.setIdcard(teacher.getIdcard());
		if(teacher.getPoliticalStatus().length() != 0)t.setPoliticalStatus(teacher.getPoliticalStatus());
		if(teacher.getDegree().length() != 0)t.setDegree(teacher.getDegree());
		if(teacher.getNation().length() != 0)t.setNation(teacher.getNation());
		
		teacherDAO.attachDirty(t);
		
		BeanUtils.copyProperties(t, teacher);
		
		result.put(Constant.SUCCESS, null);
		return result;
	}
}
